'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:AttendanceAttendanceOutsideCtrl
 * @description
 * # AttendanceAttendanceOutsideCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('AttendanceAttendanceOutsideCtrl', function (DataService, localStorageService, openApi, MapService, Util) {
        var vm = this;
        // 获取考勤详情
        var checkinfo = {};
        checkinfo = localStorageService.get("checkinfo");
        vm.param = Util.getLocalInfo("attParam");
        vm.type = "外勤";
        this.submit = function () {
            if (!vm.type) {
                Util.Toast("请选择签到类型")
                return
            }
            // 获取当前位置信息
            MapService.getcurPositon().then(function (data) {
                var curPoint = data;
                var attPoint = {};
                var ifPermission;
                attPoint.latitude = checkinfo.lat;
                attPoint.longitude = checkinfo.lng;
                vm.param.distance = MapService.getDistance(attPoint, curPoint);
                ifPermission = MapService.permissionDistance(attPoint, curPoint, checkinfo.offSet);
                if (ifPermission) {
                    //范围内则返回
                    Util.Toast("范围内重新打卡", {
                        callback: Util.goBack
                    })
                } else {
                    // 重新获取地址
                    vm.param.lat = curPoint.latitude;
                    vm.param.lng = curPoint.longitude;
                    vm.param.address = curPoint.address;
                    vm.param.remark = "[" + vm.type + "]" + (vm.content ? vm.content : "")
                    DataService.getData(openApi.punchClock, vm.param).then(function (data) {
                        if (data.code == 1) {
                            navigator.notification.beep(1);
                            Util.Toast("签到成功", {
                                callback: Util.goBack
                            });
                            Util.clearLocalInfo("attParam")
                        } else {
                            Util.Toast(data.result);
                        }
                    })
                }
            })
        };

    });
